package com.arvinclub.web.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

/**
 * 设置字符编码（utf-8）
 */
@WebFilter(filterName = "charFilter", urlPatterns = "/*")
public class CharactorFilter implements Filter
{
    //字符编码
    private static final String ENCODING = "utf-8";

    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain) throws IOException, ServletException
    {
        //设置request字符编码
        request.setCharacterEncoding(ENCODING);
        //设置response字符编码
        response.setContentType("text/html;charset=" + ENCODING);
        //传递给下一个过滤器
        chain.doFilter(request, response);
    }

    public void init(FilterConfig filterConfig) { }
    public void destroy() { }
}